package com.action.ui.view;

import android.content.Context;
import android.graphics.Rect;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;

import com.luseen.autolinklibrary.AutoLinkTextView;

/**
 * @Created SiberiaDante
 * @Describe：
 * @CreateTime: 2017/12/17
 * @UpDateTime:
 * @Email: 2654828081@qq.com
 * @GitHub: https://github.com/SiberiaDante
 */

public class JudgeNestedScrollView extends NestedScrollView {
    private boolean isNeedScroll = true;
    private float xDistance, yDistance, xLast, yLast;
    private int scaledTouchSlop;

    public JudgeNestedScrollView(Context context) {
        super(context, null);
    }

    public JudgeNestedScrollView(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
    }

    public JudgeNestedScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        scaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }
    private View clickView;
    OnClickListener listener=null;
    public void setClickView(View clickView) {
        this.clickView = clickView;


    }
    private boolean getAllChildViews(View view, int xLast,int yLast) {
        boolean status=false;
        if (view instanceof ViewGroup) {
            ViewGroup vp = (ViewGroup) view;
            for (int i = 0; i < vp.getChildCount(); i++) {
                View viewchild = vp.getChildAt(i);
                Rect scrollBounds = new Rect();
                int[] location = new int[2];
                viewchild.getLocationOnScreen(location);
                int xPosition = location[0];
                int yPosition = location[1];
                viewchild.getHitRect(scrollBounds);
                Rect visirect = new Rect(xPosition + 40, yPosition, xPosition + scrollBounds.width() - 80, yPosition + scrollBounds.height());
                if (visirect.contains((int) xLast, (int) yLast)) {
//                    if (AppUtils.LISTENER_INFO_OF_VIEW == null) {
//                        AppUtils.LISTENER_INFO_OF_VIEW = ReflectUtils.getField(View.class, "mListenerInfo");
//                    }
//                    Object listenerInfo = ReflectUtils.get(AppUtils.LISTENER_INFO_OF_VIEW, viewchild);
//                    if(listenerInfo!=null) {
//                        if (AppUtils.LISTENER_OF_CLICK == null) {
//                            AppUtils.LISTENER_OF_CLICK = ReflectUtils.getField(listenerInfo.getClass(), "mOnClickListener");
//                        }
//                        listener = ReflectUtils.get(AppUtils.LISTENER_OF_CLICK, listenerInfo);
//                        if (listener != null || getAllChildViews(viewchild, xLast, yLast)) {
//                            status = true;
//                            break;
//                        }
//                    }
                    if(viewchild instanceof RecyclerView){
                        status=true;
                        break;
                    }
                    if(viewchild instanceof AutoLinkTextView){
                        status=true;
                        break;
                    }
                }

            }
        }
        return status;
    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                xDistance = yDistance = 0f;
                xLast = ev.getX();
                yLast = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE: {
                final float curX = ev.getX();
                final float curY = ev.getY();
                xDistance += Math.abs(curX - xLast);
                yDistance += Math.abs(curY - yLast);
                xLast = curX;
                yLast = curY;
                Rect scrollBounds = new Rect();
                int[] location = new int[2];
                clickView.getLocationOnScreen(location);
                int xPosition = location[0];
                int yPosition = location[1];
                clickView.getHitRect(scrollBounds);
                Rect visirect = new Rect(xPosition + 40, yPosition, xPosition + scrollBounds.width() - 80, yPosition + scrollBounds.height());
                if (getAllChildViews(clickView,(int) xLast, (int) yLast)) {
                    Log.e("SiberiaDante", "contain xy");
                    return false;
                }
                Log.e("SiberiaDante", "xDistance ：" + xDistance + "---yDistance:" + yDistance);
                return !(xDistance > yDistance || yDistance < scaledTouchSlop) && isNeedScroll;
            }
        }
        return super.onInterceptTouchEvent(ev);
    }

    /*
    改方法用来处理NestedScrollView是否拦截滑动事件
     */
    public void setNeedScroll(boolean isNeedScroll) {
        this.isNeedScroll = isNeedScroll;
    }
}
